<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .container{
            margin: 20px auto;
            width: 550px;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            
        }
        .card{
            width: 150px;
            margin-top: 20px;
            border: 1px red solid;
        }
        img{
            height: 110px;
        }
        .mask{
            position: absolute;
            display: flex;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color:rgba(0, 0, 0, .5);
        }
        .box{
            width: 170px;
            padding: 10px;
            margin: auto;
            background-color: #fff;
        }
    </style>
</head>
<body>
    
    <div id="app">
        <cards :list="list"></cards>
        <masks></masks>
    </div>

    <template id="cards">
        <div class="container">
            <div class="card" v-for="(item, index) in list" :key="index">
                <img :src="item.url">
                <h3>{{item.title}}</h3>
                <button @click="sendflag(list, index)">查看详情</button>
            </div>
        </div>
    </template>

    <template id="masks">
        <div class="mask" v-show="flag" @click.self="flag = !flag">
            <div class="box">
                <img :src="data.url">
                <h3 name="title" v-if="!isedit">{{data.title}}</h3>
                <input type="text" name="title" v-else v-model="data.title">
                <p name="info" v-if="!isedit">{{data.info}}</p>
                <textarea name="info"  rows="10" v-else v-model="data.info"></textarea>
                <button @click="isedit = !isedit">{{isedit ? '确认' : '编辑'}}</button>
                <button @click="deldata" v-show="!isedit">删除</button>
            </div>
        </div>
    </template>

    <script src="../lib/Mock/mock.js"></script>
    <script src="../lib/Vue/vue.js"></script>

    <script>

        Vue.prototype.$bus = new Vue();

        var vm = new Vue({
            el: '#app',
            data: {
                list: []
            },
            created() {
                var Random = Mock.Random;
                for (let i = 0; i < 5; i++) {
                    var obj = {
                        url: Random.dataImage('200x150', '随机图片'),
                        title: Random.ctitle(),
                        info: Random.cparagraph()
                    }
                    this.list.push(obj)
                }
            },
            components: {
                cards: {
                    template: '#cards',
                    props: ['list'],
                    data() {
                        return {
                            
                        }
                    },
                    methods: {
                        sendflag(list, index) {
                            this.$bus.$emit('flagmove', list, index)
                        }
                    },
                },
                masks: {
                    template: '#masks',
                    data() {
                        return {
                            flag: false,
                            data: {},
                            list: [],
                            index: '',
                            isedit: false,
                            info: '',
                            
                        }
                    },
                    created() {
                        this.$bus.$on('flagmove', (list, index) => {
                            this.flag = !this.flag;
                            this.data = list[index];
                            this.list = list;
                            this.index = index
                        })
                    },
                    methods: {
                        deldata() {
                            this.list.splice(this.index, 1);
                            this.flag = !this.flag
                        }
                    },
                }
            }
        })
    </script>
</body>
</html>